package cn.orange.ch01_array;

import java.util.Arrays;

/**
 * LC977.有序数组的平方
 */
public class LC977 {
    public int[] sortedSquares(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        int[] ans = new int[nums.length];
        int n = nums.length - 1;
        while (left <= right) {
            if (nums[left] * nums[left] > nums[right] * nums[right]) {
                ans[n] = nums[left] * nums[left];
                left++;
            } else {
                ans[n] = nums[right] * nums[right];
                right--;
            }
            n--;
        }
        return ans;
    }

    public static void main(String[] args) {
        LC977 alg = new LC977();
        System.out.println(Arrays.toString(alg.sortedSquares(new int[]{-4, -1, 0, 3, 10})));
        System.out.println(Arrays.toString(alg.sortedSquares(new int[]{-7, -3, 2, 3, 11})));
    }
}
